package com.sun.myproject.Operaservlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.myproject.bean.Book;
import com.sun.myproject.bean.User;
import com.sun.myproject.dao.BookDao;
import com.sun.myproject.dao.OperaBookDao;
import com.sun.myproject.dao.UserDao;
import com.sun.myproject.daoimpl.BookDaoImpl;
import com.sun.myproject.daoimpl.OperaBookDaoImpl;
import com.sun.myproject.daoimpl.UserDaoImpl;
import com.sun.myproject.tool.TextUtils;


@WebServlet("/BorrowBooksServlet")
public class BorrowBooksServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public BorrowBooksServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("hehehe");
		try {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			//1.接收id
			int Bookid = Integer.parseInt(request.getParameter("bookid"));
			System.out.println(Bookid);
			//2.查询数据
			BookDao Dao = new BookDaoImpl();
			Book Book = Dao.findBookById(Bookid);
			System.out.println(Book.toString());
			//3.显示数据
			//存数据
			request.setAttribute("Book", Book);
			String userid2=request.getParameter("userid2");
			System.out.println("---------------------------------------------"+userid2);
			if(TextUtils.isEmpty(userid2)) {
				System.out.println("??????????????????????????????????????????????");
				request.getRequestDispatcher("bookManager/BorrowBook.jsp").forward(request, response);
			}else {
				System.out.println("////////////////////////////////////////////////////");
				int userid = Integer.parseInt(request.getParameter("userid2"));
			//2.查询数据
			UserDao UserDao = new UserDaoImpl();
			User user = UserDao.findUserById(userid);
			System.out.println(user.toString());
			//3. 显示数据
			//存数据
			request.setAttribute("user", user);
			OperaBookDao bookDao = new OperaBookDaoImpl();
			bookDao.BorrowBooks(Book, user, new Date());
			request.getRequestDispatcher("/BorrowNoteServlet").forward(request, response);
			}	
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
